package com.xxl.mq.client.factory.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.xxl.mq.client.consumer.IMqConsumer;
import com.xxl.mq.client.consumer.annotation.MqConsumer;
import com.xxl.mq.client.factory.XxlMqClientFactory;

/**
 * @author xuxueli 2018-11-18 21:18:10
 */
public class XxlMqSpringClientFactory implements ApplicationContextAware, DisposableBean {
	// ---------------------- param ----------------------
	private String adminAddress;
	private String accessToken;

	public void setAdminAddress(String adminAddress) {
		this.adminAddress = adminAddress;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	// XxlMqClientFactory
	private XxlMqClientFactory xxlMqClientFactory;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// load consumer from spring
		List<IMqConsumer> consumerList = new ArrayList<>();
		Map<String, Object> serviceMap = applicationContext.getBeansWithAnnotation(MqConsumer.class);
		if (serviceMap != null && serviceMap.size() > 0) {
			for (Object serviceBean : serviceMap.values()) {
				if (serviceBean instanceof IMqConsumer) {
					consumerList.add((IMqConsumer) serviceBean);
				}
			}
		}
		// init
		xxlMqClientFactory = new XxlMqClientFactory();
		xxlMqClientFactory.setAdminAddress(adminAddress);
		xxlMqClientFactory.setAccessToken(accessToken);
		xxlMqClientFactory.setConsumerList(consumerList);
		xxlMqClientFactory.init();
	}

	@Override
	public void destroy() throws Exception {
		xxlMqClientFactory.destroy();
	}
}
